/**
 * 
 */
package com.lorizz.picpub.task.impl;

import java.awt.image.BufferedImage;

import com.lorizz.picpub.services.ImageService;
import com.lorizz.picpub.services.ImageStore;
import com.lorizz.upm.ImageThumbnailTask;
import com.lorizz.upm.ImageVariant;

/**
 * @author wind hong
 *
 */
public class ImageThumbnailTaskExecutor extends AbstractTaskExecutor<ImageThumbnailTask> {
    
    private ImageService imageService;
    private ImageStore imageStore;
    private String storeCategory;

    public void setImageService(ImageService imageService) {
        this.imageService = imageService;
    }

    public void setImageStore(ImageStore imageStore) {
        this.imageStore = imageStore;
    }

    public void setStoreCategory(String storeCategory) {
		this.storeCategory = storeCategory;
	}

	@Override
    protected void executeTask(ImageThumbnailTask task) {
        BufferedImage ori = imageStore.load(task.getImage().getUri());
        BufferedImage cropped = imageService.crop(ori, task.getWidth(), task.getPercentage());
        String uri = imageStore.store(storeCategory, cropped);
        ImageVariant variant = task.getImage().createImageVariant();
        variant.setWidth(cropped.getWidth());
        variant.setHeight(cropped.getHeight());
        variant.setName(task.getName());
        variant.setUri(uri);
        upmRepository.persist(variant);
    }

}
